<template>
  <span class="MCheckbox" @click="change()">
    <div :class="['border',checked?'border-actived':'']">
      <transition name="trans">
        <div class="chosen" v-show="checked"></div>
      </transition>
    </div>
    <slot></slot>
    <input :name="inputname" :value="value" :checked="checked" type="checkbox" />
  </span>
</template>

<style lang="less" scoped>
    @import "~core/basic.less";

    .MCheckbox {
      display: inline-block;
      cursor: pointer;

      .noselect();
    }

    .border {
      margin-right: @gird;
      display: inline-block;
      width: 2*@gird;
      height: 2*@gird;
      border: 2px solid @color;
      vertical-align: middle;
      text-align: center;

      transition: all 500ms @ease-strong;
    }

    .border-actived {
      background: @color;
      
    }

    .chosen {
      margin-left: 1px;
      margin-top: 1px;
      transform: rotate(-45deg);
    }

    .chosen:before {
      content: "";
      display: block;
      margin: 0px;
      width: 3px;
      height: 6px;
      background: @white;
    }

    .chosen:after {
      content: "";
      display: block;
      margin: 0px;
      width: 15px;
      height: 3px;
      background: @white;
    }

    input {
      display: none;
    }

    .trans-enter-active, .trans-leave-active {
      transition: all 500ms;
      opacity: 1;
      transform: scale(1,1) rotate(-45deg);
      transition-timing-function: @ease-strong;
    }
    .trans-enter, .trans-leave-active {
      opacity: 0;
      transform: scale(0,0);
    }
</style>

<script>
  export default {
    data(){
      return {
        inputname:'',
        checked:false
      }
    },
    props:{
      name:String,
      value:String
    },
    mounted(){
      if(!this.$parent.name || this.$parent.$el._prevClass!="MGroup"){
        throw new Error("MRadio need to inside MGroup component witch should have a property of name.");
        return;
      }
      this.inputname = this.$parent.name;
    },
    methods:{
      change(){
        this.checked = ~this.checked;
        if(Object.prototype.toString.call(this.$parent.data) != '[object Array]'){
          this.$parent.data = new Array();
        }
        if(this.checked){
          this.$parent.data.push(this.value);
        }else{
          var list = this.$parent.data;
          for(var i=0; i<list.length; i++){
            if(list[i]==this.value){
              list.splice(i,1);
            }
          }
        }
      }
    }
  }
</script>